rsubmit;
data sic; set comp.company (keep = gvkey sic); proc sort; by gvkey;
data cusip; set comp.security (keep = gvkey cusip); proc sort; by gvkey;
data temp.ind; merge sic cusip; by gvkey;
if sic<1000 and sic not in (100, 116, 139, 173, 780) and (130<=sic<=133)=0 then cons=1;
proc print; where cons=1 and sic ne ''; run;

rsubmit;
data delist; set crsp.mse (keep = permno date event dlret dlstcd);
where event="DELIST" and dlret<10 and 1962<year(date)<2021;
year=year(date); month=month(date); drop date; proc sort; by permno year month;
data crsp; set crsp.msf (keep = permno cusip shrout hsiccd date ret prc);
where 1962<year(date)<2021; year=year(date); month=month(date);
quar=qtr(date); price=abs(prc); sic=hsiccd; me=abs(prc)*shrout;
proc sort; by permno year month; data crsp; merge crsp delist;
by permno year month; drop date prc shrout; proc means; run;

data exch; set crsp.mse (keep = date permno exchcd shrcd siccd);
year=year(date); month=month(date); drop date;
proc sort data=exch; by permno year month;
data crsp; merge crsp exch; by permno year month;
data temp.crsp; set crsp; by permno year month; i=0; do while(i<2);
lexchcd = lag(exchcd); lsiccd = lag(siccd); lshrcd=lag(shrcd);
if permno = lag(permno) and exchcd = . then exchcd = lexchcd;
if permno = lag(permno) and shrcd = . then shrcd = lshrcd;
if permno = lag(permno) and siccd = . then siccd = lsiccd;
i+1; end; if last.month then output; drop i lexchcd lshrcd lsiccd;
proc sort data=temp.crsp nodupkey; by permno year month;
data temp.crsp; set temp.crsp;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and (exchcd ne 0 
and exchcd ne 3 and exchcd ne 33) then dlret=-0.3;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and
(exchcd=0 or exchcd=3 or exchcd=33) then dlret=-0.55;
if nmiss(ret)=1 and nmiss(dlret)=0 then return=dlret*100;
else return=ret*100; if shrcd ne 10 and shrcd ne 11 then delete;
drop event ret dlret dlstcd shrcd hsiccd; proc means; run;

rsubmit;
data crsp; set temp.crsp; if sic<1000 and sic not in (100, 116, 139, 173, 780) and (130<=sic<=133)=0 then cons=1;
if sic=1490 then cons=1; if 1000<=sic<200 and sic ne 1490 and sic not in (1020, 1450, 1470) and (1200<=sic<=1240)=0 then inv=1;
if sic in (2295, 2299, 2400, 2420, 2421, 2426, 2429, 2430, 2431, 2434, 2435, 2436, 2450, 2451, 2452,
2491, 2493, 2520, 2521, 2522, 2530, 2540, 2541, 2542, 2590, 2599, 2610, 2819, 2893, 2895, 2950, 2951, 2952) then inv=1;
if sic in (2499, 2500, 2510, 2511, 2512, 2514, 2515, 2517, 2519, 2591, 2730, 2731) or 2000<=sic<2100
and sic not in (2044, 2070, 2074, 2075, 2076, 2077, 2083) or sic in (2100, 2110, 2120, 2130) or 2200<=sic<2400
and sic not in (2290, 2295, 2296, 2297, 2299) or sic in (2490, 2499) then cons=1; if sic in (2295, 2299) then inv=1;
if 2400<=sic<2500 and sic not in (2410, 2439, 2440, 2441, 2448, 2449, 2490, 2499) then inv=1;
if 2520<=sic<2545 or sic=2590 or sic=2599 then inv=1; if 2500<=sic<2520 or sic=2591 then cons=1;
if sic=2610 then inv=1; if 2600<=sic<2700 and sic not in (2610, 2620, 2630, 2675, 2677) then cons=1;
if sic in (2800, 2830, 2840, 2841, 2842, 2844, 2861, 2870, 2879, 2890, 2891, 2899) then cons=1;
if sic in (2700, 2710, 2720, 2730, 2731, 2740, 2770, 2780, 2782) then cons=1;
if sic in (2819, 2893, 2895) then inv=1; if sic in (2950, 2951, 2952) then inv=1;
if sic in (2900, 2910, 2990, 2992) then cons=1; if sic in (3000, 3010, 3020, 3050, 3052, 3053, 3080) then cons=1;
if 3200<=sic<=3230 or sic in (3260, 3262, 3263, 3269, 3280, 3290, 3291, 3295, 3299) and sic ne 3221 then cons=1;
if sic=3221 or 3240<=sic<3260 and sic ne 3250 and sic ne 3255 or sic in (3261, 3264, 3292, 3296, 3297) or 3270<=sic<3280
then inv=1; if 3100<=sic<3200 and sic ne 3110 and sic ne 3130 then cons=1; if 3300<=sic<=3355 or sic=3357 then inv=1;
if sic in (3400, 3420, 3421, 3423, 3429, 3430, 3433, 3466, 3469, 3484, 3493, 3495, 3496) then cons=1;
if sic in (3425, 3432, 3440, 3442, 3443, 3444, 3449, 3470, 3471, 3479, 3490, 3491, 3492, 3494, 3498, 3499) then inv=1;
if 3500<=sic<3599 and sic not in (3519, 3533, 3562, 3566, 3568, 3590, 3592) then inv=1;
if 3610<=sic<3630 or sic=3643 or sic=3644 or 3660<=sic<3670 or sic=3699 then inv=1;
if sic=3600 or 3630<=sic<3660 or 3690<=sic<3699 then cons=1; if sic in (3713, 3715, 3740, 3799) then inv=1;
if 3700<=sic<3720 or sic in (3732, 3750, 3792) then cons=1; if sic in (3850, 3870, 3842) then cons=1;
if sic in (3713, 3715, 3740, 3799) then inv=1; if sic in (3842, 3850, 3870) then cons=1;
if sic=3800 or 3820<=sic<3850 and sic ne 3822 and sic ne 3842 or sic=3860 then inv=1; if sic=3993 then inv=1;
if 3910<=sic<=3952 or 3960<=sic<=3991 or sic=3999 then cons=1; if sic=8710 then inv=1;
if 4000<=sic<4400 or sic=4500 or 4100<=sic<4730 or 4740<=sic<=4780 or sic=4789 or 4800<=sic<4999 then cons=1;
if 7000<=sic<7310 or 7320<=sic<7350 or sic=7380 or 7383<=sic<7999 then cons=1;
if 8000<=sic<8700 or sic=8733 then cons=1; proc means; run;

rsubmit;
data crsp; set crsp; lme=lag(me); if cusip ne lag(cusip) then lme=.;
if year<1964 or year>2020 then delete; proc sort; by year month;
proc means data=crsp noprint; var return; by year month; weight lme;
where cons=1; output out=consvw mean(return) = consvw; run;
proc means data=crsp noprint; var return; by year month;
where cons=1; output out=consew mean(return) = consew; run;
proc means data=crsp noprint; var return; by year month; weight lme;
where inv=1; output out=invvw mean(return) = invvw; run;
proc means data=crsp noprint; var return; by year month;
where inv=1; output out=invew mean(return) = invew; run;
data ret; merge consvw consew invvw invew; by year month;
drop _freq_ _type_; proc means; run;
proc download data=ret out=sasuser.factor5 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR5 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcret"; RUN;

rsubmit;
data crsp; set temp.crsp; if siccd<1000 and siccd not in (100, 116, 139, 173, 780) and (130<=siccd<=133)=0 then cons=1;
if siccd=1490 then cons=1; if 1000<=siccd<200 and siccd ne 1490 and siccd not in (1020, 1450, 1470) and (1200<=siccd<=1240)=0 then inv=1;
if siccd in (2295, 2299, 2400, 2420, 2421, 2426, 2429, 2430, 2431, 2434, 2435, 2436, 2450, 2451, 2452,
2491, 2493, 2520, 2521, 2522, 2530, 2540, 2541, 2542, 2590, 2599, 2610, 2819, 2893, 2895, 2950, 2951, 2952) then inv=1;
if siccd in (2499, 2500, 2510, 2511, 2512, 2514, 2515, 2517, 2519, 2591, 2730, 2731) or 2000<=siccd<2100
and siccd not in (2044, 2070, 2074, 2075, 2076, 2077, 2083) or siccd in (2100, 2110, 2120, 2130) or 2200<=siccd<2400
and siccd not in (2290, 2295, 2296, 2297, 2299) or siccd in (2490, 2499) then cons=1; if siccd in (2295, 2299) then inv=1;
if 2400<=siccd<2500 and siccd not in (2410, 2439, 2440, 2441, 2448, 2449, 2490, 2499) then inv=1;
if 2520<=siccd<2545 or siccd=2590 or siccd=2599 then inv=1; if 2500<=siccd<2520 or siccd=2591 then cons=1;
if siccd=2610 then inv=1; if 2600<=siccd<2700 and siccd not in (2610, 2620, 2630, 2675, 2677) then cons=1;
if siccd in (2800, 2830, 2840, 2841, 2842, 2844, 2861, 2870, 2879, 2890, 2891, 2899) then cons=1;
if siccd in (2700, 2710, 2720, 2730, 2731, 2740, 2770, 2780, 2782) then cons=1;
if siccd in (2819, 2893, 2895) then inv=1; if siccd in (2950, 2951, 2952) then inv=1;
if siccd in (2900, 2910, 2990, 2992) then cons=1; if siccd in (3000, 3010, 3020, 3050, 3052, 3053, 3080) then cons=1;
if 3200<=siccd<=3230 or siccd in (3260, 3262, 3263, 3269, 3280, 3290, 3291, 3295, 3299) and siccd ne 3221 then cons=1;
if siccd=3221 or 3240<=siccd<3260 and siccd ne 3250 and siccd ne 3255 or siccd in (3261, 3264, 3292, 3296, 3297) or 3270<=siccd<3280
then inv=1; if 3100<=siccd<3200 and siccd ne 3110 and siccd ne 3130 then cons=1; if 3300<=siccd<=3355 or siccd=3357 then inv=1;
if siccd in (3400, 3420, 3421, 3423, 3429, 3430, 3433, 3466, 3469, 3484, 3493, 3495, 3496) then cons=1;
if siccd in (3425, 3432, 3440, 3442, 3443, 3444, 3449, 3470, 3471, 3479, 3490, 3491, 3492, 3494, 3498, 3499) then inv=1;
if 3500<=siccd<3599 and siccd not in (3519, 3533, 3562, 3566, 3568, 3590, 3592) then inv=1;
if 3610<=siccd<3630 or siccd=3643 or siccd=3644 or 3660<=siccd<3670 or siccd=3699 then inv=1;
if siccd=3600 or 3630<=siccd<3660 or 3690<=siccd<3699 then cons=1; if siccd in (3713, 3715, 3740, 3799) then inv=1;
if 3700<=siccd<3720 or siccd in (3732, 3750, 3792) then cons=1; if siccd in (3850, 3870, 3842) then cons=1;
if siccd in (3713, 3715, 3740, 3799) then inv=1; if siccd in (3842, 3850, 3870) then cons=1;
if siccd=3800 or 3820<=siccd<3850 and siccd ne 3822 and siccd ne 3842 or siccd=3860 then inv=1; if siccd=3993 then inv=1;
if 3910<=siccd<=3952 or 3960<=siccd<=3991 or siccd=3999 then cons=1; if siccd=8710 then inv=1;
if 4000<=siccd<4400 or siccd=4500 or 4100<=siccd<4730 or 4740<=siccd<=4780 or siccd=4789 or 4800<=siccd<4999 then cons=1;
if 7000<=siccd<7310 or 7320<=siccd<7350 or siccd=7380 or 7383<=siccd<7999 then cons=1;
if 8000<=siccd<8700 or siccd=8733 then cons=1; proc means; run;

rsubmit;
data crsp; set crsp; lme=lag(me); if cusip ne lag(cusip) then lme=.;
if year<1964 or year>2020 then delete; proc sort; by year month;
proc means data=crsp noprint; var return; by year month; weight lme;
where cons=1; output out=consvw mean(return) = consvw; run;
proc means data=crsp noprint; var return; by year month;
where cons=1; output out=consew mean(return) = consew; run;
proc means data=crsp noprint; var return; by year month; weight lme;
where inv=1; output out=invvw mean(return) = invvw; run;
proc means data=crsp noprint; var return; by year month;
where inv=1; output out=invew mean(return) = invew; run;
data ret; merge consvw consew invvw invew; by year month;
drop _freq_ _type_; proc means; run;
proc download data=ret out=sasuser.factor5 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR5 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcevent"; RUN;

PROC IMPORT OUT=sasuser.imc
DATAFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls" 
DBMS=EXCEL REPLACE; RANGE="imcfactor$"; run;
rsubmit;
proc upload data=sasuser.imc out=temp.imc (replace=yes); run;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by year month;
proc upload data=sasuser.imc out=temp.imc (replace=yes);
data factors; set ff.factors_monthly
(keep = year month mktrf rf smb hml); proc sort; by year month;
data factors; merge factors temp.imc; by year month;
mkt=mktrf*100; smbf=smb*100; hmlf=hml*100; drop mktrf smb hml;
data crsp; merge crsp (in=in1) factors;
by year month; if in1 and year>1963 and year<2021;
yearm=(year-1963)*12+month; eret=((1+return/100)/(1+rf)-1)*100;
drop return year month rf; proc means; run;

rsubmit;
options nosource nonotes errors=0;
data crspall; set crsp; drop imcew imceventvw imceventew;
%let begyear=49; %let endyear=696;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crspall (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crspall; if yearm ge %eval(&myear-36)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.imcvw
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt imcvw / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta imcvw;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=49; %let endyear=696;
%macro makebeta(st); data temp.betaimcvw; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.betaimcvw; set temp.betaimcvw;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
options source notes errors=5;
data return; set temp.crsp (keep = cusip year month return me price);
data return; set return; lme=lag(me); if cusip ne lag(cusip) then lme=.;
lprice=lag(price); if cusip ne lag(cusip) then lprice=.;
if year<1964 or year>2020 then delete; proc sort; by cusip year month;
data ivol; merge return temp.betaimcvw; by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint; var imcvw; by year month; where lprice ge 5;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  imcdecile=1;
  IF imcvw > DEC20 THEN imcdecile=2;
  IF imcvw > DEC40 THEN imcdecile=3;
  IF imcvw > DEC60 THEN imcdecile=4;
  IF imcvw > DEC80 THEN imcdecile=5;
  if nmiss(imcvw) or lprice<5 then imcdecile=.;
drop dec20 dec40 dec60 dec80;
proc tabulate data=ivol format=5.3;
class imcdecile; var return;
table return, (imcdecile all)*mean; run;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 5;
proc means data=ivol noprint;
var return; by year month; weight lme;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcbetavw"; RUN;
rsubmit;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 5;
proc means data=ivol noprint;
var return; by year month;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcbetaew"; RUN;

PROC IMPORT OUT=sasuser.imc
DATAFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls" 
DBMS=EXCEL REPLACE; RANGE="imcfactor$"; run;
rsubmit;
proc upload data=sasuser.imc out=temp.imc (replace=yes); run;
data crsp; set temp.crsp (keep = cusip year month return);
proc sort data=crsp; by year month;
proc upload data=sasuser.imc out=temp.imc (replace=yes);
data factors; set ff.factors_monthly
(keep = year month mktrf rf smb hml); proc sort; by year month;
data factors; merge factors temp.imc; by year month;
mkt=mktrf*100; smbf=smb*100; hmlf=hml*100; drop mktrf smb hml;
data crsp; merge crsp (in=in1) factors;
by year month; if in1 and year>1963 and year<2021;
yearm=(year-1963)*12+month; eret=((1+return/100)/(1+rf)-1)*100;
drop return year month rf; proc means; run;

rsubmit;
options nosource nonotes errors=0;
data crspall; set crsp; drop imcvw imceventvw imceventew;
%let begyear=49; %let endyear=696;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crspall (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crspall; if yearm ge %eval(&myear-36)
and yearm le %eval(&myear-1); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.imcew
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=temp.ivol&myear noprint;
model eret = mkt imcew / edf; by cusip;
data temp.ivol&myear; set temp.ivol&myear;
if _edf_+_p_<24 then delete; yearm = %eval(&myear);
beta=mkt; keep cusip yearm beta imcew;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=49; %let endyear=696;
%macro makebeta(st); data temp.betaimcew; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.betaimcew; set temp.betaimcew;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
options source notes errors=5;
data return; set temp.crsp (keep = cusip year month return me);
data return; set return; lme=lag(me); if cusip ne lag(cusip) then lme=.;
if year<1964 or year>2020 then delete; proc sort; by cusip year month;
data ivol; merge return temp.betaimcew; by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint; var imcew; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  imcdecile=1;
  IF imcew > DEC20 THEN imcdecile=2;
  IF imcew > DEC40 THEN imcdecile=3;
  IF imcew > DEC60 THEN imcdecile=4;
  IF imcew > DEC80 THEN imcdecile=5;
  if nmiss(imcew) then imcdecile=.;
drop dec20 dec40 dec60 dec80;
proc tabulate data=ivol format=5.3;
class imcdecile; var return;
table return, (imcdecile all)*mean; run;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 5;
proc means data=ivol noprint;
var return; by year month; weight lme;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcewbetavw"; RUN;
rsubmit;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 5;
proc means data=ivol noprint;
var return; by year month;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcewbetaew"; RUN;

rsubmit;
options source notes errors=5;
data return; set temp.crsp (keep = cusip year month return me price);
data return; set return; lme=lag(me); if cusip ne lag(cusip) then lme=.;
lprice=lag(price); if cusip ne lag(cusip) then lprice=.;
if year<1964 or year>2020 then delete; proc sort; by cusip year month;
data ivol; merge return temp.betaimcvw; by cusip year month; proc sort; by year month;
proc univariate data=ivol noprint; var imcvw; by year month; where lprice ge 5;
output out=decile pctlpts = 10 to 90 by 10 pctlpre=dec;
DATA ivol; MERGE ivol decile; BY year month;
  imcdecile=1;
  IF imcvw > DEC10 THEN imcdecile=2;
  IF imcvw > DEC20 THEN imcdecile=3;
  IF imcvw > DEC30 THEN imcdecile=4;
  IF imcvw > DEC40 THEN imcdecile=5;
  IF imcvw > DEC50 THEN imcdecile=6;
  IF imcvw > DEC60 THEN imcdecile=7;
  IF imcvw > DEC70 THEN imcdecile=8;
  IF imcvw > DEC80 THEN imcdecile=9;
  IF imcvw > DEC90 THEN imcdecile=10;
  if nmiss(imcvw) or lprice<5 then imcdecile=.;
drop dec20 dec40 dec60 dec80 dec10 dec30 dec50 dec70 dec90;
proc tabulate data=ivol format=5.3;
class imcdecile; var return;
table return, (imcdecile all)*mean; run;

rsubmit;
options nosource nonotes errors=0;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month; weight lme;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcbeta10vw"; RUN;
rsubmit;
proc sort data=ivol; by year month imcdecile;
%macro a(many); %do i=1 %to 10;
proc means data=ivol noprint;
var return; by year month;
where imcdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4
factor5 factor6 factor7 factor8 factor9 factor10;
by year month; if nmiss(month) then delete;
if year<1968 or year>2020 then delete;
drop _freq_ _type_; proc means data=temp.factor; run;
proc sort; by year month;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA= SASUSER.FACTOR25 
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2021.xls"
DBMS=EXCEL REPLACE; SHEET="imcbeta10ew"; RUN;



